/*
 * Copyright (c) 2017,安徽皖通科技股份有限公司 All rights reserved.
 */

package com.wttech.dzzf.process.service.initilize;

import com.wtkj.vo.dzzf.base.kfwd.CardInfo;
import com.wttech.dzzf.process.dao.DataInitilizingDao;
import com.wttech.dzzf.process.model.user.User;
import com.wttech.dzzf.process.service.user.UserDetailInfosCreatorService;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;

import static com.wttech.dzzf.process.constant.UserConstants.DEFAULT_PROCESS_NUM;
import static com.wttech.dzzf.process.constant.UserConstants.OLD_OWNERTYPE_COMPANY;
import static com.wttech.dzzf.process.constant.UserConstants.OLD_OWNERTYPE_USER;

/**
 * 卡片信息处理拆分服务
 *
 * @author MaShibo
 * @date 2017/12/13.
 */
@Service
public class SubCardInfoProcesssService {

	private static final Logger LOGGER = LoggerFactory.getLogger(SubCardInfoProcesssService.class);

	@Resource
	private DataInitilizingDao dataInitilizingDao;

	@Resource
	private UserDetailInfosCreatorService userDetailInfosCreatorService;

	void processPeriodOneOnce(int num, String endOn) {
		LOGGER.info("第{}次，开始执行...", num);
		int startRow = (num - 1) * DEFAULT_PROCESS_NUM + 1;
		int endRow = num * DEFAULT_PROCESS_NUM;
		List<CardInfo> cardInfos = dataInitilizingDao.queryOnePeriod(startRow, endRow, endOn);
		cardInfos.forEach(this::processCardInfo);
		LOGGER.info("第{}次，结束执行...", num);
	}

	void processPeriodTwoOnce(int num, String startOn, String endOn) {
		LOGGER.info("第{}次，开始执行...", num);
		int startRow = (num - 1) * DEFAULT_PROCESS_NUM + 1;
		int endRow = num * DEFAULT_PROCESS_NUM;
		List<CardInfo> cardInfos = dataInitilizingDao.queryTwoPeriod(startRow, endRow, startOn, endOn);
		cardInfos.forEach(this::processCardInfo);
		LOGGER.info("第{}次，结束执行...", num);
	}

	void processCardInfo(CardInfo cardInfo) {
		Validate.notNull(cardInfo, "卡片信息不能为空");
		String cardInfoId = cardInfo.getCardInfoId();
		String userId = cardInfo.getUserId();
		if (StringUtils.isBlank(userId)) {
			LOGGER.info("卡片【{}】没有生成客户信息ID", cardInfoId);
			return;
		}
		Object ownerTypeObj = cardInfo.getExtendProperties().get("OWNERTYPE");
		if (ownerTypeObj == null) {
			LOGGER.error("卡片【{}】无法确定用户类型", cardInfoId);
			return;
		}
		Integer ownerType = Integer.valueOf(ownerTypeObj.toString());
		User user = null;
		if (ownerType == OLD_OWNERTYPE_USER) {
			user = dataInitilizingDao.queryUserInfo(userId);
			if (user == null) {
				user = dataInitilizingDao.queryCompanyInfo(userId);
			}
		} else if (ownerType == OLD_OWNERTYPE_COMPANY) {
			user = dataInitilizingDao.queryCompanyInfo(userId);
			if (user == null) {
				user = dataInitilizingDao.queryUserInfo(userId);
			}
		}
		if (user == null) {
			LOGGER.info("卡片【{}】找不到对应的客户信息", cardInfoId);
			return;
		}
		userDetailInfosCreatorService.dealWithCard(user, cardInfo);
	}

	public void process() {
		List<CardInfo> cardInfos = dataInitilizingDao.queryCardInfosByIds();
		try {
			cardInfos.forEach(this::processCardInfo);
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
	}
}
